<?php
class Backend extends CI_Controller {
	 var $permObj;

		 function __construct()
		{

			parent::__construct();

			$this->load->helper('cookie');
			$this->load->library('encrypt');

			require_once('Zend/Acl.php');
			require_once('Zend/Acl/Role.php');
			require_once('Zend/Acl/Resource.php');
			$acl = new Zend_Acl();

			$acl->addRole(new Zend_Acl_Role('admin'));
			$acl->addRole(new Zend_Acl_Role('customer'));
			$acl->addRole(new Zend_Acl_Role('hotel'));

			$acl->add(new Zend_Acl_Resource('admin'));
			$acl->add(new Zend_Acl_Resource('admin/settings'), 'admin');
			$acl->add(new Zend_Acl_Resource('admin/user'), 'admin');
			$acl->add(new Zend_Acl_Resource('admin/add_language'), 'admin');

			

			$acl->add(new Zend_Acl_Resource('hotel'));
			$acl->add(new Zend_Acl_Resource('hotel/settings'), 'hotel');
			$acl->add(new Zend_Acl_Resource('hotel/add'), 'hotel');
			$acl->add(new Zend_Acl_Resource('hotel/edit'), 'hotel');
			$acl->add(new Zend_Acl_Resource('hotel/managebook'), 'hotel');

			$acl->add(new Zend_Acl_Resource('customer'));

			$acl->add(new Zend_Acl_Resource('default'));

			$acl->allow('admin');
			$acl->allow('hotel', 'hotel');
			$acl->allow('customer', 'customer');
			
			$this->permObj = $acl;

			

		}

		function index()
		{
			$my_role = $this->encrypt->decode(get_cookie('role'));

			if ( $my_role == 'customer' )
				die;
			if ( $my_role == 'admin' )
				header('location:http://'.$_SERVER['HTTP_HOST'].'/hotelbooking/index.php/backend/admin');

			if ( $this->encrypt->decode(get_cookie('m_name')) != get_cookie('mName') )
				die('Are u Hacker ? !!!');

			if( $my_role != '' )
			{
				$data['role'] = $my_role;
				$data['mName'] = get_cookie('mName');

				$config = $this->config->item('db_config');
				$this->load->model('Dbmodel', '', $config);
				
				// find  ID MEMBER who input the data
				$idMember = $this->Dbmodel->select_where('member', array('mem_user' => $this->encrypt->decode(get_cookie('m_name'))), 'mem_id');

	//			$objRowHotel = $this->Dbmodel->select_where( 'hotel', array('mem_id' => $idMember[0]->mem_id));
				$objRowHotel = $this->Dbmodel->select_join("hotel", "room_type", "hotel.hotel_id,hotel.hotel_name,room_type.rt_id,room_type.rt_name", "room_type.hotel_id = hotel.hotel_id", array('hotel.mem_id' => $idMember[0]->mem_id), "hotel_id");

			
		
				if ( $objRowHotel )
				{
					for($i=0; $i<count($objRowHotel); $i++)
					{
							$data['displayHotel'][$i] = array(
								 'hotel_id' => $objRowHotel[$i]->hotel_id ,
								 'hotel_name' => $objRowHotel[$i]->hotel_name ,
								'rt_id' => $objRowHotel[$i]->rt_id ,
								'rt_name' => $objRowHotel[$i]->rt_name ,
							);
	
					}
				}
				else
				{
					$data['notice'] = 'you may not insert hotel or room yet !!!';
				}
				

				$this->load->view('main_backend', $data);
			}
		}

		function admin($segment_1='', $segment_2='')
		{
			$admin_role = $this->encrypt->decode(get_cookie('role'));

			if ( $this->encrypt->decode(get_cookie('m_name')) != get_cookie('mName') )
				die('Are u Hacker ? !!!');
			else if ( $admin_role != 'admin' )
				die('Still Trying ????');


			if ($segment_1 != '')
				$segment_1 = '/'.$segment_1;
						
		   $permit = $this->permObj->isAllowed($admin_role, 'admin'.$segment_1);
			
			if ($permit)
			{
					$config = $this->config->item('db_config');
					$this->load->model('Dbmodel', '', $config);
					
					if ( $segment_1 == '/add_language' )
					{
						
					}
					elseif ( $segment_1 == '/user' )
					{
							if ( strstr($segment_2, 'del_') )
							{
								$id_mem = (int)str_replace('del_', '', $segment_2);
								// delete member
								$this->Dbmodel->delete_entry('member', 'mem_id', $id_mem);

								$objHotel = $this->Dbmodel->select_where('hotel', array('mem_id' => $id_mem), 'hotel_id');
								if( $objHotel )
								{
										for($i=0; $i<count($objHotel); $i++)
										{
												$this->Dbmodel->delete_entry('room_type', 'hotel_id', (int)$objHotel[$i]->hotel_id);
												$this->Dbmodel->delete_entry('hotel_option', 'hotel_id', (int)$objHotel[$i]->hotel_id);
										}
								}

								// delete hotel 
								$this->Dbmodel->delete_entry('hotel', 'mem_id', $id_mem);

								header('location:http://'.$_SERVER['HTTP_HOST'].'/hotelbooking/index.php/backend/admin/user');
							}
							else
							{
									$objMember = $this->Dbmodel->select_where('member', array('mem_role' => 'hotel'));
									// list all MEMBER to show
									if( $objMember )
									{
											for($i=0; $i<count($objMember); $i++)
											{
												$data['listMember'][$i] = array(
														'id' => $objMember[$i]->mem_id ,
														'name' => $objMember[$i]->mem_user ,
														'fname' => $objMember[$i]->mem_fname ,
														'email' => $objMember[$i]->mem_email ,
													);
											}									
									}
							}
							

							$data['func'] = 'user'; 
							$this->load->view('admin_backend', $data);
					}
					else
					{
								$data['func'] = 'hotel';
								$objHotel = $this->Dbmodel->multi_join("hotel", "hotel.hotel_id,hotel.hotel_name,room_type.rt_id,room_type.rt_name,hotel.mem_id,member.mem_user,member.mem_fname,member.mem_email", "room_type:room_type.hotel_id = hotel.hotel_id,member:hotel.mem_id=member.mem_id", "", "hotel_id");
								
								if ($objHotel)
								{
											
								/*			$data['listMember'][0] = array(
														'id' => $objHotel[0]->mem_id ,
														'name' => $objHotel[0]->mem_user ,
														'fname' => $objHotel[0]->mem_fname ,
														'email' => $objHotel[0]->mem_email ,
													); */

											
											$listMember = array();

											$temp_mem_id = $objHotel[0]->mem_id;
											$j = 0;

												for($i=0; $i<count($objHotel); $i++)
												{
												/*		if( $objHotel[$i]->mem_id != $temp_mem_id )
														{
															
															$data['listMember'][$j] = array(
																'id' => $objHotel[$i]->mem_id ,
																'name' => $objHotel[$i]->mem_user ,
																'fname' => $objHotel[$i]->mem_fname ,
																'email' => $objHotel[$i]->mem_email ,
															); 

															$data['listMember'][] = array($objHotel[$i]->mem_user => $objHotel[$i]->hotel_name);
													

															$temp_mem_id = $objHotel[$i]->mem_id;
														} */

														if(!array_key_exists($objHotel[$i]->hotel_name, $listMember))
														{
															$j++;
										//					$listMember[] = array($objHotel[$i]->mem_user => $objHotel[$i]->hotel_name);
															$listMember[$objHotel[$i]->hotel_name] = $objHotel[$i]->mem_user;
														}

														$data['displayHotel'][$i] = array(
															 'hotel_id' => $objHotel[$i]->hotel_id ,
															 'hotel_name' => $objHotel[$i]->hotel_name ,
															'rt_id' => $objHotel[$i]->rt_id ,
															'rt_name' => $objHotel[$i]->rt_name ,
															'mem_id' => $objHotel[$i]->mem_id ,
															'mem_user' => $objHotel[$i]->mem_user ,
														);
				
												}
												$data['listMember'] = $listMember;
								//				print_r($data['displayHotel']);
								//				die;
										$op = '';	
										$this->load->view('admin_backend', $data);
								}
					}
			}
			else
				echo 'No permission';
		}
		function hotel($segment_1='', $segment_2='', $segment_3='')
		{
			$data['grant'] = 'no';
			$data['insertHotel'] = false;
			$data['hotelToEdit'] = null;
			$data['roomToEdit'] = null;

			$role = $this->encrypt->decode(get_cookie('role'));		
			if ( $this->encrypt->decode(get_cookie('m_name')) != get_cookie('mName') )
				die('Are u Hacker ? !!!');
			else if ( $role != 'hotel' && $role != 'admin' )
				die('Still Trying ????');

			if ($segment_1 != '')
				$segment_1 = '/'.$segment_1;


		   $permit = $this->permObj->isAllowed($role, 'hotel'.$segment_1);
			
			if ($permit)
			{
				$op = '';
	//			echo 'Grant access';
				$data['grant'] = 'hotel';
				$data['arr_option'] = array();
				$data['addRoom'] = false;
				$data['editRoom'] = false;

					$arr_fac = array(
						'1' => 'Swimming Pool' ,
						'2' => 'Gym/Fitness' ,
						'3' => 'Car Park' ,
						'4' => 'Spa/Sauna'
					);

				$config = $this->config->item('db_config');
				$this->load->model('Dbmodel', '', $config);

				

// **************************** start Edit section ****************************
				if ( strstr($segment_1, 'edit') )
				{
					if ( strstr($segment_2, 'room') )
					{
						if ( $this->input->post('submit_rt') == 'Edit Room' )
						{
								$update_room = array(
									'rt_name' => $this->security->xss_clean($this->input->post('rt_name')) ,
								   'rt_description' => $this->security->xss_clean($this->input->post('rt_description')) ,
								   'rt_quantity' => $this->security->xss_clean($this->input->post('rt_quantity')) ,
								   'rt_price' => $this->security->xss_clean($this->input->post('rt_price')) ,		
								);
								$this->Dbmodel->update_entry('room_type', array('rt_id' => $this->security->xss_clean($this->input->post('rt_id'))), $update_room);
								header('location:http://'.$_SERVER['HTTP_HOST'].'/hotelbooking/index.php/backend/');
						}
						else
						{
								$room_id = (int)$this->encrypt->decode(str_replace('room.', '', $segment_2));
								$objRowRoom = $this->Dbmodel->select_where( 'room_type', array('rt_id' => $room_id ));
								if ($objRowRoom)
								{
									$data['roomToEdit'] = array(
						//						'id' => $objRowRoom[0]->rt_id ,
												'name' => $objRowRoom[0]->rt_name ,
												'description' => $objRowRoom[0]->rt_description ,
												'quantity' => $objRowRoom[0]->rt_quantity ,
												'price' => $objRowRoom[0]->rt_price ,
				
											);
									$data['editRoom'] = true;
									$data['rt_id'] = $objRowRoom[0]->rt_id;
								}
						}
						
					}
					else
					{
							if ( $this->input->post('submit_hotel') == 'Edit' )
							{
								$update_hotel = array(
									'hotel_name' => $this->security->xss_clean($this->input->post('hotel_name')) ,
								   'hotel_city' => $this->security->xss_clean($this->input->post('hotel_city')) ,
								   'hotel_description' => $this->security->xss_clean($this->input->post('hotel_description')) ,
								   'hotel_location' => $this->security->xss_clean($this->input->post('hotel_location')) ,
								   'hotel_address' => $this->security->xss_clean($this->input->post('hotel_addr')) ,
									'hotel_hint' => $this->security->xss_clean($this->input->post('hotel_hint')) ,
								);
								$this->Dbmodel->update_entry('hotel', array('hotel_id' => $this->security->xss_clean($this->input->post('hotel_id'))), $update_hotel);

								$updateChk = '';
								for($i=0; $i<count($this->input->post('fac')); $i++)
								{
										$tempFac = $this->input->post('fac');
										if ( array_search($tempFac[$i], $arr_fac) > -1 )
												$updateChk .= array_search($tempFac[$i], $arr_fac).',';			
								}
								$update_option = array(
											'hotel_opt_name' => $updateChk
										);
								$this->Dbmodel->update_entry('hotel_option', array('hotel_id' => $this->security->xss_clean($this->input->post('hotel_id'))), $update_option);
								header('location:http://'.$_SERVER['HTTP_HOST'].'/hotelbooking/index.php/backend/');
						
							}
							else  // display data in DB for Edit
							{
								   
									// find  ID MEMBER who input the data
									$idMember = $this->Dbmodel->select_where('member', array('mem_user' => $this->encrypt->decode(get_cookie('m_name'))), 'mem_id');
									
									$hotel_id = (int)$segment_2;
									// retrieve hotel
									$objRowHotel = $this->Dbmodel->select_where( 'hotel', array('mem_id' => $idMember[0]->mem_id, 'hotel_id' => $hotel_id));

									if ($objRowHotel)
									{
											$data['hotelToEdit'] = array(
													'id' => $objRowHotel[0]->hotel_id ,
													'name' => $objRowHotel[0]->hotel_name ,
													'city' => $objRowHotel[0]->hotel_city ,
													'description' => $objRowHotel[0]->hotel_description ,
													'location' => $objRowHotel[0]->hotel_location ,
													'address' => $objRowHotel[0]->hotel_address ,
													'hint' => $objRowHotel[0]->hotel_hint ,
												);
											
											// retrieve hotel option
											$objRowHotelOption = $this->Dbmodel->select_where( 'hotel_option', array('hotel_id' => $data['hotelToEdit']['id']));
								
											if ($objRowHotelOption)
											{
													$no_opt = explode(',', $objRowHotelOption[0]->hotel_opt_name);
													for($i=0; $i<count($no_opt); $i++)
													{
														if($no_opt[$i] != '')
														{
															$data['arr_option'][] = $arr_fac[$no_opt[$i]];
														}
													}						
											}
									}
							}
					}

				}
  //  ************************************* end Edit section **************************
    //  ************************************* start Add section **************************
				if (strstr($segment_1, 'add') && $segment_2 == 'room')
				{
					 if ($this->input->post('submit_rt') == 'Add Room')
					{
							
							if ( $this->input->post('hotel_id') != '' )
							{
									$hotel_id = $this->encrypt->decode($this->security->xss_clean($this->input->post('hotel_id')));
									// insert Room type data
									$insertRoomData  = array(
										   'rt_name' => $this->security->xss_clean($this->input->post('rt_name')) ,		
										   'rt_description' => $this->security->xss_clean($this->input->post('rt_description')) ,
										   'rt_quantity' => $this->security->xss_clean($this->input->post('rt_quantity')) ,
										   'rt_price' => $this->security->xss_clean($this->input->post('rt_price')) ,
										   'hotel_id' => $hotel_id
										);
									$this->Dbmodel->insert_entry('room_type', $insertRoomData);
									header('location:http://'.$_SERVER['HTTP_HOST'].'/hotelbooking/index.php/backend/');
							}
						
					}
					else
					{
							$data['addRoom'] = true;
							if ($segment_3 != '')
							{
			//						$hotel_id = $this->encrypt->decode($segment_3);
									$data['h_id'] = $segment_3;
							}

					}
	
				}
				elseif ( strstr($segment_1, 'add') )
				{
						if ( $this->input->post('submit_hotel') == 'Add' )
						{
									// find  ID MEMBER who input the data
									$idMember = $this->Dbmodel->select_where('member', array('mem_user' => $this->encrypt->decode(get_cookie('m_name'))), 'mem_id');
									
									// insert Hotel data
									$insertData  = array(
											'hotel_name' => $this->security->xss_clean($this->input->post('hotel_name')) ,
										   'hotel_city' => $this->security->xss_clean($this->input->post('hotel_city')) ,
										   'hotel_description' => $this->security->xss_clean($this->input->post('hotel_description')) ,
										   'hotel_location' => $this->security->xss_clean($this->input->post('hotel_location')) ,
										   'hotel_address' => $this->security->xss_clean($this->input->post('hotel_addr')) ,
											'hotel_hint' => $this->security->xss_clean($this->input->post('hotel_hint')) ,
											'mem_id' => $idMember[0]->mem_id ,
										);
									$this->Dbmodel->insert_entry('hotel', $insertData);

									// find ID Hotel 
									$idHotel = $this->Dbmodel->get_last_entries( 'hotel', 1, "hotel_id desc", 'hotel_id');

									if ( $idHotel )
											$newIdHotel = (int)($idHotel[0]->hotel_id);
								
									$temp_fac = '';

									//  insert Hotel facilities 
									for($i=0; $i<count($this->input->post('fac')); $i++)
									{
											$tempFac = $this->input->post('fac');
											$temp_fac .= array_search($tempFac[$i], $arr_fac).',';						
									}
									if ( !empty($temp_fac) )
									{
										$dataFac = array(
												'hotel_opt_name' => $temp_fac ,
												'hotel_id' => $newIdHotel
											);
										$this->Dbmodel->insert_entry('hotel_option', $dataFac);
									}

									$data['insertHotel'] = true;
									$data['h_id'] = $newIdHotel;
						}
				}
				elseif( strstr($segment_1, 'managebook') )
				{
					if ( $segment_2 == 'delete' && $segment_3 != '' )
					{
							$this->Dbmodel->delete_entry('booking', 'booking_id', (int)$this->encrypt->decode($segment_3));
							header('location:http://'.$_SERVER['HTTP_HOST'].'/hotelbooking/index.php/backend/hotel/managebook/');
					}
					$idMember = $this->Dbmodel->select_where('member', array('mem_user' => $this->encrypt->decode(get_cookie('m_name'))), 'mem_id');
					$objRowBooking = $this->Dbmodel->select_join("hotel", "booking", "", "booking.hotel_id = hotel.hotel_id", array('mem_id' => $idMember[0]->mem_id));
					if($objRowBooking)
					{
						for($i=0; $i<count($objRowBooking); $i++)
						{
								$from_date = date('Y-m-d',$objRowBooking[$i]->booking_date_start);
								$to_date = date('Y-m-d',$objRowBooking[$i]->booking_date_end);
								$data['dataBooking'][$i] = array(
										 'hotel_id' => $objRowBooking[$i]->hotel_id ,
										 'hotel_name' => $objRowBooking[$i]->hotel_name ,
										'hotel_description' => $objRowBooking[$i]->hotel_description ,
										'hotel_city' => $objRowBooking[$i]->hotel_city ,
										'mem_id' => $objRowBooking[$i]->mem_id ,
										'hotel_address' => $objRowBooking[$i]->hotel_address ,
										'rt_id' => $objRowBooking[$i]->rt_id ,
										'booking_date_start' => $from_date ,
										'booking_date_end' => $to_date ,
										'booking_amount_date' => $objRowBooking[$i]->booking_amount_date ,
										'client_id' => $objRowBooking[$i]->client_id ,
										'booking_id' => $objRowBooking[$i]->booking_id ,
									);
						}
						$this->load->view('managebook_view', $data);
						$op = 'managebook';
					}

				}
				if ( $op != 'managebook' )
					$this->load->view('backendview', $data);
			}
			else
				echo 'No permission';
		}
		function customer($segment_1='')
		{
			
			$role = $this->encrypt->decode(get_cookie('role'));		
			if ( $this->encrypt->decode(get_cookie('m_name')) != get_cookie('mName') )
				die('Are u Hacker ? !!!');
//			else if ( $role != 'hotel' && $role != 'admin' )
//				die('Still Trying ????');

			if ($segment_1 != '')
				$segment_1 = '/'.$segment_1;
			
			$permit = $this->permObj->isAllowed($role, 'customer'.$segment_1);
//		   $permit = $this->permObj->isAllowed('customer', $segment_1);
			
			if ($permit)
			{
				$data['heading'] = 'Client Page';
				$this->load->view('clientview', $data);

			}
			else
				echo 'No permission';
		}


/*		function settings($segment_1)
		{		

			$role = 'hotel';

		   $permit = $this->permObj->isAllowed($role, $segment_1);
			
			if ($permit)
				echo 'Grant access';
			else
				echo 'No permission , For '.$segment_1.' Only !!!';
		} */

}
?>